<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <el-input v-model="query.name" clearable placeholder="模板名称" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <el-select v-model="query.frameworkType" filterable clearable class="filter-item" placeholder="请选择" @change="crud.toQuery">
          <el-option
            v-for="item in dict.framework_type"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="550px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="110px">
          <el-form-item label="模板名称" prop="name">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="镜像源url" prop="imagePushUrl">
            <el-input v-model="form.imagePushUrl" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="框架类型" prop="frameworkType">
            <el-select v-model="form.frameworkType" style="width: 370px;" filterable placeholder="请选择">
              <el-option
                v-for="item in dict.framework_type"
                :key="item.id"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="Jenkins实例" prop="jenkinsId">
            <el-select v-model="form.jenkinsId" style="width: 370px;" filterable placeholder="请选择">
              <el-option
                v-for="item in jenkinsList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!-- 修改参数表单 -->
      <el-dialog :close-on-click-modal="false" :visible.sync="showParamForm" title="修改参数表单" width="750px">
        <el-form ref="paramContextForm" :model="paramContextForm" size="small" label-width="0px">
          <el-form-item label="">
            <el-input v-model="paramContextForm.paramForm" :rows="16" type="textarea" style="width: 700px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="showParamForm = false">取消</el-button>
          <el-button type="primary" @click="saveParam">确认</el-button>
        </div>
      </el-dialog>
      <!-- 修改job参数 -->
      <el-dialog :close-on-click-modal="false" :visible.sync="showJobContextForm" title="修改Job" width="750px">
        <el-form ref="jobContextForm" :model="jobContextForm" size="small" label-width="0px">
          <el-form-item label="">
            <el-input v-model="jobContextForm.content" :rows="16" type="textarea" style="width: 700px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="showJobContextForm = false">取消</el-button>
          <el-button type="primary" @click="saveJob">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="名称" width="150" />
        <el-table-column prop="frameworkType" label="框架类型" width="80" />
        <el-table-column prop="imagePushUrl" label="镜像源url" />
        <el-table-column prop="jenkinsName" label="Jenkins实例" width="150" />
        <el-table-column v-if="checkPer(['admin','dpImageBuildTpl:edit','dpImageBuildTpl:del'])" label="操作" width="500px" align="center">
          <template slot-scope="scope">
            <udOperation :data="scope.row" :permission="permission">
              <el-button
                slot="left"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                :disabled="false"
                style="margin-right: 3px;"
                @click="editParam(scope.row)"
              >
                参数
              </el-button>
              <el-button
                slot="left"
                type="primary"
                icon="el-icon-edit"
                size="mini"
                :disabled="false"
                @click="editJob(scope.row)"
              >
                脚本
              </el-button>
            </udOperation>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDpImageBuildTpl from '@/api/deploy/dpImageBuildTpl'
import { getJenkinsList } from '@/api/deploy/dpJenkinsInstance'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { mapGetters } from 'vuex'

const defaultForm = { id: null, env: null, name: null, frameworkType: null, jenkinsId: null, paramForm: null, content: null, createBy: null, updateBy: null, createTime: null, updateTime: null }
export default {
  name: 'DpImageBuildTpl',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['framework_type'],
  cruds() {
    return CRUD({ title: '镜像构建模板表', url: 'api/dpImageBuildTpl', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDpImageBuildTpl }})
  },
  data() {
    return {
      jenkinsList: [],
      paramContextForm: { id: null, paramForm: null },
      jobContextForm: { id: null, content: null },
      showParamForm: false,
      showJobContextForm: false,
      permission: {
        add: ['admin', 'dpImageBuildTpl:add'],
        edit: ['admin', 'dpImageBuildTpl:edit'],
        del: ['admin', 'dpImageBuildTpl:del']
      },
      rules: {
        env: [
          { required: true, message: '环境不能为空', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '模板名称不能为空', trigger: 'blur' }
        ],
        imagePushUrl: [
          { required: true, message: '镜像源url不能为空', trigger: 'blur' }
        ],
        frameworkType: [
          { required: true, message: '框架类型不能为空', trigger: 'blur' }
        ],
        jenkinsId: [
          { required: true, message: 'Jenkins实例id不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'env', display_name: '环境' },
        { key: 'name', display_name: '模板名称' },
        { key: 'frameworkType', display_name: '框架类型' },
        { key: 'jenkinsId', display_name: 'Jenkins实例id' }
      ]
    }
  },
  computed: {
    ...mapGetters([
      'env'
    ])
  },
  created() {
    this.$nextTick(this.refreshJenkinsList)
    // 监听应用下拉框选择事件
    this.$bus.$on('global-env-change', this.crud.toQuery)
    this.$bus.$on('global-env-change', this.refreshJenkinsList)
  },

  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.query.env = this.env
      return true
    },
    [CRUD.HOOK.beforeToAdd]() {
      this.form.env = this.env
      return true
    },
    refreshJenkinsList() {
      getJenkinsList(this.env).then(data => {
        this.jenkinsList = data
      })
    },
    editParam(data) {
      this.showParamForm = true
      this.paramContextForm.id = data.id
      this.paramContextForm.paramForm = data.paramForm
    },
    editJob(data) {
      this.showJobContextForm = true
      this.jobContextForm.id = data.id
      this.jobContextForm.content = data.content
    },
    saveJob() {
      crudDpImageBuildTpl.updateJobOnly(this.jobContextForm).then(() => {
        this.showJobContextForm = false
        this.crud.toQuery()
      }
      )
    },
    saveParam() {
      crudDpImageBuildTpl.updateParamFormOnly(this.paramContextForm).then(() => {
        this.showParamForm = false
        this.crud.toQuery()
      }
      )
    }
  }
}
</script>

<style scoped>
</style>
